package com.petvc.tv

import android.content.Context
import android.os.Build
import android.provider.Settings
import java.net.Inet4Address
import java.net.NetworkInterface

object DeviceUtils {
    // 获取设备指纹（纯数字）
    fun getDeviceId(context: Context): String {
        val androidId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        return androidId.filter { it.isDigit() }.ifEmpty { androidId.hashCode().toString().filter { it.isDigit() } }
    }

    // 获取设备名称
    fun getDeviceName(): String {
        return Build.MODEL ?: "PetTV设备"
    }

    // 获取本机IP地址
    fun getLocalIpAddress(): String? {
        try {
            val interfaces = NetworkInterface.getNetworkInterfaces()
            for (intf in interfaces) {
                val addrs = intf.inetAddresses
                for (addr in addrs) {
                    if (!addr.isLoopbackAddress && addr is Inet4Address) {
                        return addr.hostAddress
                    }
                }
            }
        } catch (ex: Exception) {}
        return null
    }

    // 获取本机MAC地址
    fun getMacAddress(): String? {
        try {
            val all = NetworkInterface.getNetworkInterfaces()
            for (nif in all) {
                if (!nif.name.equals("wlan0", ignoreCase = true)) continue
                val macBytes = nif.hardwareAddress ?: return null
                return macBytes.joinToString(":") { "%02X".format(it) }
            }
        } catch (ex: Exception) {}
        return null
    }
} 